﻿using Dapper;
using Infrastructure.DataBaseConnect;
using Infrastructure.IRepository;
using Model.DomainModel;

namespace Infrastructure.Repository
{
    /// <summary>
    /// 流程申请审核记录仓储接口实现
    /// </summary>
    public class FlowApplyRecordRepository : Repository<FlowApplyRecord>, IFlowApplyRecordRepository
    {
        /// <summary>
        /// 根据流程申请id获取审核信息
        /// </summary>
        /// <param name="flowApplyId">流程申请id</param>
        /// <returns></returns>
        public List<FlowApplyRecord> GetFlowApplyRecordByFlowApplyId(string flowApplyId)
        {
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            string sql = " select  * from FlowApplyRecord where FlowApplyId  = @FlowApplyId order by CreateTime desc ";
            return connection.Query<FlowApplyRecord>(sql, new { FlowApplyId = flowApplyId }).ToList();
        }

        /// <summary>
        /// 根据节点id和流程申请id获取审核信息
        /// </summary>
        /// <param name="nodeId">节点ID</param>
        /// <param name="flowApplyId">流程申请id</param>
        /// <returns></returns>
        public List<FlowApplyRecord> GetFlowApplyRecordByNodeId(string nodeId, string flowApplyId)
        {
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            string sql = " select  * from FlowApplyRecord where FlowApplyId  = @FlowApplyId AND NodeId = @NodeId order by CreateTime desc ";
            return connection.Query<FlowApplyRecord>(sql, new { FlowApplyId = flowApplyId, NodeId = nodeId }).ToList();
        }

        /// <summary>
        /// 根据驳回节点id和流程申请id获取审核信息
        /// </summary>
        /// <param name="nodeId">节点ID</param>
        /// <param name="flowApplyId">流程申请id</param>
        /// <returns></returns>
        public List<FlowApplyRecord> GetFlowApplyRecordByRejectNodeIds(string rejectNodeId, string flowApplyId)
        {
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            string sql = " select  * from FlowApplyRecord where FlowApplyId  = @FlowApplyId AND RejectNodeIds  like @RejectNodeIds order by CreateTime desc ";
            return connection.Query<FlowApplyRecord>(sql, new { FlowApplyId = flowApplyId, RejectNodeIds = $"%{rejectNodeId}%" }).ToList();
        }
    }
}
